package com.icecloud.cloud.upms.conf.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

public class SecurityRestConfiguration extends WebSecurityConfigurerAdapter {
	
	@Autowired
    private IceCloudDetailsService iceCloudDetailsService;
	
	
	@Bean
	public StandardPasswordEncoder standardPasswordEncoder(){
	        return new StandardPasswordEncoder();
	}

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(iceCloudDetailsService).passwordEncoder(standardPasswordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/faq/**","/demo/company/**");
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    	http.addFilterBefore(restUserNamePasswdAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class).exceptionHandling().accessDeniedHandler(new RestAccessDeniedHandler()).
    	authenticationEntryPoint(new RestAuthenticationEntryPoint())
    	.and()
            .authorizeRequests()
            	.antMatchers("/dic/","/demo/user/").hasAnyRole("LegalPerson")
            	.antMatchers("/roles","departments").authenticated()
                .and()
            .csrf()
                .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
             	.and()
            .logout()
            	.addLogoutHandler(logoutHandler()).logoutUrl("/rest/logout");
    }
    
    
    @Bean
    public RestUserNamePasswdAuthenticationFilter restUserNamePasswdAuthenticationFilter() throws Exception{
    	RestUserNamePasswdAuthenticationFilter restUserNamePasswd = new RestUserNamePasswdAuthenticationFilter();
    	restUserNamePasswd.setAuthenticationManager(authenticationManagerBean());
    	restUserNamePasswd.setAuthenticationSuccessHandler(new RestAuthenticationSuccessHandler());
    	restUserNamePasswd.setAuthenticationFailureHandler(new RestAuthenticationFailureHandler());
        return restUserNamePasswd;
    }
    
    @Bean
	public LogoutHandler logoutHandler(){
	        return new RestLogoutHandler();
	}

	
}
